package com.dmp.onlinestore.model.dao.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.ArrayList;

import com.dmp.onlinestore.model.dao.ProductDao;
import com.dmp.onlinestore.model.dao.jdbc.base.*;
import com.dmp.onlinestore.model.dao.base.*;
import com.dmp.onlinestore.model.dao.jdbc.base.JdbcDaoTransaction;
import com.dmp.onlinestore.model.dto.ProductBean;
import com.dmp.onlinestore.model.dto.ProductBeanFilterSpec;
import com.dmp.onlinestore.system.Settings;

public class JdbcProductDao extends JdbcDao implements ProductDao {
	
	public JdbcProductDao(DaoTransaction transaction) {
		super((JdbcDaoTransaction) transaction);
	}
	
	// TODO Need to implement filtering for JdbcProductDao.
	public List<ProductBean> getProducts(ProductBeanFilterSpec filter) throws DaoException {
		if (Settings.CONSOLE_DEBUG_ON) {
			System.out.println("JdbcProductDao::getProducts");
		}
		String sql = "SELECT * from product;";
		Statement stmt = null;
		ResultSet rs = null;
		try {
			List<ProductBean> productList = new ArrayList<ProductBean>();
			Connection conn = getTransaction().getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				ProductBean product = new ProductBean(
						rs.getInt("id"),
						rs.getString("name"),
						rs.getString("short_desc"),
						rs.getString("long_desc"),
						rs.getString("category"),
						rs.getString("subcategory"),
						rs.getDouble("price"),
						rs.getInt("rating"));
                productList.add(product);
			}
            rs.close();
            stmt.close();  
			return(productList);
		}
		catch (SQLException e) {
			logSQLException(e);
	        DriverUtilities.forceCloseResultSet(rs);
	        DriverUtilities.forceCloseStatement(stmt);
	        getTransaction().fail();
	        throw new DaoException();     
		}
	}
	
	public ProductBean getProduct(int id) throws DaoException {
		if (Settings.CONSOLE_DEBUG_ON) {
			System.out.println("JdbcProductDao::getProduct");
		}
        String sql = "SELECT * from product where id = " + id;
        Statement stmt = null;
        ResultSet rs = null;
		try {
			Connection conn = getTransaction().getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			ProductBean product = null;
            if (rs.next()) {
            	// There was one.
				product = new ProductBean(
					rs.getInt("id"),
					rs.getString("name"),
					rs.getString("short_desc"),
					rs.getString("long_desc"),
					rs.getString("category"),
					rs.getString("subcategory"),
					rs.getDouble("price"),
					rs.getInt("rating"));
            }
            rs.close();
            stmt.close();  
            return product;
		}
		catch (SQLException e) {
			logSQLException(e);
	        DriverUtilities.forceCloseResultSet(rs);
	        DriverUtilities.forceCloseStatement(stmt);
	        getTransaction().fail();
	        throw new DaoException(); 
		}
	}
	
//	public List<Product> getProducts_static() {
//
//		List<Product> prodList = new ArrayList<Product>();
//		// ------------------------------
//		Product prod1 = new Product();
//		prod1.setId(1111);
//		prod1.setName("Product_1111");
//		prod1.setShortDescription("Product 1111 short desc");
//		prod1.setLongDescription("Product 1111 long desc");
//		// ------------------------------
//		Product prod2 = new Product();
//		prod2.setId(2222);
//		prod2.setName("Product_2222");
//		prod2.setShortDescription("Product 2222 short desc");
//		prod2.setLongDescription("Product 2222 long desc");
//		// ------------------------------
//		prodList.add(prod1);
//		prodList.add(prod2);
//		return prodList;
//	}
//	
//	public Product getProduct_static(int id) {
//		Product prod3 = new Product();
//		prod3.setId(id);
//		prod3.setName("Product_"+id);
//		prod3.setShortDescription("Product "+id+" short desc");
//		prod3.setLongDescription("Product "+id+" long desc");
//		prod3.setPrice((double) id);
//		return prod3;		
//	}
}